﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

using NUnit.Framework;

using Microsoft.Practices.Unity;

using ERPStore.Extensions;

namespace ERPStore.NewsLetters.Tests.Services
{
	[TestFixture]
	public class EmailSenderTests : ERPStore.Tests.TestBase
	{
		private int m_CampaignId;

		protected ERPStore.NewsLetters.Services.NewsLettersService NewsLetterService { get; set; }

		[SetUp]
		public override void Initialize()
		{
			base.Initialize();

			var container = ERPStore.GlobalConfiguration.Configuration.DependencyResolver.GetContainer();
			container.RegisterType<ERPStore.NewsLetters.Services.IEmailListSourceProvider, TestRecipientSourceProvider>("testrecipientsourceprovider", new ContainerControlledLifetimeManager());

			NewsLetterService = GlobalConfiguration.Configuration.DependencyResolver.GetService<ERPStore.NewsLetters.Services.NewsLettersService>();

			// PREREQUIS
			var smtpServer = GetNewPersistedSMTPServer();
			var source = GetNewPersistedEmailListSource();
			var newsLetter = GetNewPersistedNewsLetter();

			var campaign = NewsLetterService.CreateCampaign();
			campaign.Description = Guid.NewGuid().ToString();
			campaign.EmailListSourceId = source.Id;
			campaign.Name = Guid.NewGuid().ToString();
			campaign.NewsLetterId = newsLetter.Id;
			campaign.PublicKey = Guid.NewGuid().ToString();
			campaign.ReplyToEmail = "test@test.com";
			campaign.ReplyToEmailName = "test";
			campaign.SMTPServerId = smtpServer.Id;
			campaign.State = Models.SendState.Pending;

			NewsLetterService.SaveCampaign(campaign);
			m_CampaignId = campaign.Id;

			var settings = GlobalConfiguration.Configuration.DependencyResolver.GetService<Models.Settings>();
			var parameters = new Dictionary<string, object>();
			parameters.Add("StartDate", DateTime.Now);
			parameters.Add("Campaign", campaign);
			parameters.Add("Settings", settings);

			var activity = new Workflows.ScheduleCampaign();

			CloudSoft.Workflows.ManualWorkflowWorkItem.Run(
				activity
				, parameters
			);
		}

		[Test]
		public void Test_Send_NewsLetter()
		{
			var settings = GlobalConfiguration.Configuration.DependencyResolver.GetService<Models.Settings>();

			var parameters = new Dictionary<string, object>();
			parameters.Add("Settings", settings);

			CloudSoft.Workflows.ManualWorkflowWorkItem.Run(
				new Workflows.EmailSender()
				, parameters
			);

			// Waiting 5 seconds
			System.Threading.Thread.Sleep(10 * 1000);

			var filter = new Models.EmailSentListFilter();
			filter.CampaignId = m_CampaignId;
			var list = NewsLetterService.GetEmailSentDetailList(filter);

			Assert.AreNotEqual(filter.TotalCount, 0);
		}

		public Models.NewsLetter CreateValidNewsLetter()
		{
			var newsLetter = NewsLetterService.CreateNewsLetter();
			newsLetter.Content = Guid.NewGuid().ToString();
			newsLetter.FileName = Guid.NewGuid().ToString();

			return newsLetter;
		}

		public Models.EmailListSource CreateValidEmailListSource()
		{
			var list = NewsLetterService.GetEmailListSourceTypeList();
			var source = NewsLetterService.CreateEmailListSource(list.First(i => i.Key == "69EFEC43-EFA8-409B-85DA-C107442F65EC").Key);
			source.Name = Guid.NewGuid().ToString();

			return source;
		}

		public Models.EmailListSource GetNewPersistedEmailListSource()
		{
			var result = CreateValidEmailListSource();
			NewsLetterService.SaveEmailListSource(result);
			return result;
		}

		public Models.NewsLetter GetNewPersistedNewsLetter()
		{
			var newsLetter = CreateValidNewsLetter();
			NewsLetterService.SaveNewsLetter(newsLetter);
			return newsLetter;
		}

		public Models.SMTPSettings CreateValidSMTPServer()
		{
			var result = NewsLetterService.CreateSMTPServer();
			result.Name = Guid.NewGuid().ToString();

			return result;
		}


		public Models.SMTPSettings GetNewPersistedSMTPServer()
		{
			var result = CreateValidSMTPServer();
			NewsLetterService.SaveSMTPServer(result);
			return result;
		}

	}
}
